package com.tjvib.sensor;

import com.tjvib.util.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LocalSensorData extends SensorData {

    public float[] rotationVector = new float[5];

    @Override
    void reset() {
        super.reset();
        for (int i = 0; i < 5; i++) rotationVector[i] = 0;
    }

    public LocalSensorData() {
        reset();
    }

    public LocalSensorData(LocalSensorData d) {
        super(d);
        for (int i = 0; i < 5; i++) rotationVector[i] = d.rotationVector[i];
    }

    public LocalSensorData(JSONObject jsonObject) throws JSONException {
        super(jsonObject);
        JSONArray rotationVectorArray = jsonObject.getJSONArray("rotationVector");
        for (int i = 0; i < 5; i++) {
            rotationVector[i] = (float) rotationVectorArray.getDouble(i);
        }
    }

    @Override
    public JSONObject toJsonObject() throws JSONException {

        JSONObject result = new JSONObject();
        result.put("timestamp", timestamp);
        result.put("localtime", localtime);

        JSONArray gyrArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            gyrArray.put(gyr[i]);
        }
        result.put("gyr", gyrArray);

        JSONArray accArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            accArray.put(acc[i]);
        }
        result.put("acc", accArray);

        JSONArray magArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            magArray.put(mag[i]);
        }
        result.put("mag", magArray);

        JSONArray quatArray = new JSONArray();
        for (int i = 0; i < 4; i++) {
            quatArray.put(quat[i]);
        }
        result.put("quat", quatArray);

        JSONArray linAccArray = new JSONArray();
        for (int i = 0; i < 3; i++) {
            linAccArray.put(linAcc[i]);
        }
        result.put("linAcc", linAccArray);

        JSONArray rotationVectorArray = new JSONArray();
        for (int i = 0; i < 5; i++) {
            rotationVectorArray.put(rotationVector[i]);
        }
        result.put("rotationVector", rotationVectorArray);
        return result;
    }
}
